package com.example.demo.service.impl;

import com.example.demo.exception.BusyException;
import com.example.demo.mapper.IdempotentMapper;
import com.example.demo.entity.Idempotent;
import com.example.demo.service.IdempotentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Service
public class IdempotentServiceImpl implements IdempotentService {

    @Autowired
    private IdempotentMapper idempotentMapper;
    @Override
    public void insert(Idempotent idempotent) {
        int flag = 0;
        if (Objects.isNull(idempotent)) {
            return ;
        }
        try {
          flag = idempotentMapper.insert(idempotent);
        } catch (Exception exception) {
            Idempotent idempotentExist = idempotentMapper.selectByTenantIdAndParaMd5(idempotent.getTenantId(), idempotent.getParamMd5());
            if (Objects.nonNull(idempotentExist)) {
                throw new BusyException("已存在该数据，请勿重复操作");
            }
            if (flag != 1) {
                throw new BusyException("写入数据失败，请联系管理员");
            }
        }
    }
}
